package com.tanjun.mallservice.web.biz.impl;

import com.mybatisflex.core.paginate.Page;
import com.tanjun.common.constants.DefaultConstant;
import com.tanjun.common.exception.BizException;
import com.tanjun.common.model.PageInfoDto;
import com.tanjun.common.utils.ConvertUntil;
import com.tanjun.mallservice.apiservice.enums.plu.PluDescTypeEnum;
import com.tanjun.mallservice.apiservice.enums.plu.PluImgModeEnum;
import com.tanjun.mallservice.apiservice.enums.plu.PluImgTypeEnum;
import com.tanjun.mallservice.apiservice.request.plu.MallPluVo;
import com.tanjun.mallservice.apiservice.request.plu.PluIdVo;
import com.tanjun.mallservice.apiservice.request.plu.QueryPageMallPluVo;
import com.tanjun.mallservice.apiservice.response.plu.MallPluDto;
import com.tanjun.mallservice.apiservice.response.plu.MallPluSkuDetailDto;
import com.tanjun.mallservice.apiservice.response.plu.MallPluSkuDto;
import com.tanjun.mallservice.apiservice.response.plu.PluDetailDto;
import com.tanjun.mallservice.web.biz.MallPluBiz;
import com.tanjun.mallservice.web.orm.entity.MallPlu;
import com.tanjun.mallservice.web.orm.entity.MallPluImg;
import com.tanjun.mallservice.web.orm.entity.MallPluSku;
import com.tanjun.mallservice.web.service.*;
import com.tanjun.mallservice.web.service.request.MallPluRequest;
import com.tanjun.mallservice.web.service.request.QueryMallPluRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MallPluBizImpl implements MallPluBiz {
    @Autowired
    private MallPluService mallPluService;
    @Autowired
    private MallPluSkuService mallPluSkuService;
    @Autowired
    private MallPluImgService mallPluImgService;
    @Autowired
    private MallPluDescService mallPluDescService;
    @Autowired
    private MallPluSkuPriceService mallPluSkuPriceService;

    /**
     * 获取商品详细信息
     *
     * @param paramVo 商品idVO
     * @return 返回商品详细信息
     */
    @Override
    public PluDetailDto getDetailPlu(PluIdVo paramVo) {
        System.out.println("获取商品详细信息");
        MallPlu plu = mallPluService.getPluById(paramVo.getPluId());
        if (plu == null) {
            throw new BizException("商品不存在");
        }
        MallPluDto pluDto = ConvertUntil.convert(plu, MallPluDto.class);
        pluDto.setSku1ValueList(Arrays.asList(plu.getSku1Value().split(DefaultConstant.SPLIT_REGEX)));
        pluDto.setSku2ValueList(Arrays.asList(plu.getSku2Value().split(DefaultConstant.SPLIT_REGEX)));
        pluDto.setSku3ValueList(Arrays.asList(plu.getSku3Value().split(DefaultConstant.SPLIT_REGEX)));

        List<MallPluSku> listSku = mallPluSkuService.getPluSkuList(pluDto.getPluId());
        List<MallPluSkuDto> listSkuDto = ConvertUntil.convert(listSku, MallPluSkuDto.class);

        List<MallPluSkuDetailDto> listSkuDetailDto = listSkuDto.stream().map(sku -> {
            MallPluSkuDetailDto skuDetailDto = new MallPluSkuDetailDto();
            skuDetailDto.setSku(sku);
            return skuDetailDto;
        }).collect(Collectors.toList());

        PluDetailDto pluDetailDto = new PluDetailDto();
        pluDetailDto.setPlu(pluDto);
        pluDetailDto.setSkuDetailList(listSkuDetailDto);

        pluDetailDto.setDescInfo(mallPluDescService.getDescInfo(paramVo.getPluId(), null, PluDescTypeEnum.PLU));
        //设置图片
        List<MallPluImg> listImg = mallPluImgService.listImgByPluId(pluDto.getPluId());
        if (!CollectionUtils.isEmpty(listImg)) {
            //设置商品图片

            List<String> pluBannerImgList = listImg.stream().filter(t -> t.getImgType() == PluImgTypeEnum.PLU)
                    .filter(t -> t.getImgMode() == PluImgModeEnum.BANNER)
                    .map(MallPluImg::getImgUrl).collect(Collectors.toList());

            pluDetailDto.setPluBannerImgList(pluBannerImgList);
            //设置规格图片
            listSkuDetailDto.forEach(skuDetailDto -> {

                List<String> skuBannerImgList = listImg.stream()
                        .filter(t -> t.getImgType() == PluImgTypeEnum.SKU)
                        .filter(t -> t.getImgMode() == PluImgModeEnum.BANNER)
                        .filter(t -> t.getSkuId().equals(skuDetailDto.getSku().getSkuId()))
                        .map(MallPluImg::getImgUrl).collect(Collectors.toList());
                skuDetailDto.setSkuBannerImgList(skuBannerImgList);
            });
        }
        listSkuDetailDto.forEach(skuDetailDto -> {
            skuDetailDto.setSkuDescInfo(mallPluDescService.getDescInfo(paramVo.getPluId(),
                    skuDetailDto.getSku().getSkuId(),
                    PluDescTypeEnum.SKU));
        });

        //todo 设置价格(暂时不启动动态价格)
        return pluDetailDto;
    }

    /**
     * 新增商品
     *
     * @param paramVo
     */
    @Override
    public void addPlu(MallPluVo paramVo) {
        MallPluRequest pluRequest = ConvertUntil.convert(paramVo, MallPluRequest.class);
        mallPluService.saveOrUpdate(pluRequest);
    }

    /**
     * 修改商品
     *
     * @param paramVo
     */
    @Override
    public void updatePlu(MallPluVo paramVo) {
        MallPluRequest pluRequest = ConvertUntil.convert(paramVo, MallPluRequest.class);
        mallPluService.saveOrUpdate(pluRequest);
    }

    /**
     * 分页查询数据
     *
     * @param paramVo
     * @return
     */
    @Override
    public PageInfoDto<MallPluDto> listPagePlu(QueryPageMallPluVo paramVo) {
        QueryMallPluRequest request = ConvertUntil.convert(paramVo, QueryMallPluRequest.class);
        Page<MallPlu> page = mallPluService.queryPagePlu(new Page(paramVo.getPageNum(), paramVo.getPageSize()), request);
        return null;
    }
}
